package org.cgs.web.server.registration;

import java.io.IOException;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

import org.apache.log4j.Logger;
import org.cgs.web.database.DatabaseConnector;

public final class DeleteSchool extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    private static final Logger LOGGER = Logger.getLogger(DeleteSchool.class);

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        // Get School info from the web page
        String courseCode = (String)request.getSession().getAttribute("courseCode");
        boolean isInstructor = request.getParameter("isInstructor") == null ? true : Boolean.parseBoolean(request.getParameter("isInstructor"));
        String[] names = request.getParameterValues("name") == null ? new String[] {} : request.getParameterValues("name");

        // Get ready to make DB call
        boolean errorOccurred = false;
        Connection con = null;
        PreparedStatement pstmt = null;
        try
        {
            // Get instructor account from DB matching input
            con = DatabaseConnector.getInstance().getConnection();
            for (String name : names)
            {
                pstmt = con.prepareStatement("DELETE FROM school WHERE courseCode=? AND name=?");
                pstmt.setString(1, courseCode);
                pstmt.setString(2, name);
                pstmt.executeUpdate();

                try
                {
                    // Close the PreparedStatement
                    pstmt.close();
                }
                catch (Exception e)
                {
                    errorOccurred = true;
                    LOGGER.error("Exception cleaning up prepared statement", e);
                }

                pstmt = con.prepareStatement("DELETE FROM school_times WHERE courseCode=? AND name=?");
                pstmt.setString(1, courseCode);
                pstmt.setString(2, name);
                pstmt.executeUpdate();

                try
                {
                    // Close the PreparedStatement
                    pstmt.close();
                }
                catch (Exception e)
                {
                    errorOccurred = true;
                    LOGGER.error("Exception cleaning up prepared statement", e);
                }
            } // End for loop
        } // End try block
        catch (SQLException e)
        {
            errorOccurred = true;
            LOGGER.error("Exception thrown while atttempting to delete a school", e);
        }
        finally
        {
            try
            {
                // Close the PreparedStatement
                if (pstmt != null)
                {
                    pstmt.close();
                }
            }
            catch (Exception e)
            {
                errorOccurred = true;
                LOGGER.error("Exception cleaning up prepared statement", e);
            }

            try
            {
                // Close the connection
                if (con != null)
                {
                    con.close();
                }
            }
            catch (Exception e)
            {
                errorOccurred = true;
                LOGGER.error("Exception cleaning up connection to the database", e);
            }
        } // End finally

        if (errorOccurred)
        {
            request.getRequestDispatcher("").forward(request, response);
        }
        else
        {
            if (isInstructor)
            {
                response.sendRedirect("instructor/schools/deleteSchool.jsp");
            }
            else
            {
                response.sendRedirect("school/schoolMain.jsp");
            }
        }
    } // End method
} // End class
